package com.imooc.controller;


import com.imooc.pojo.Bgm;
import com.imooc.pojo.Videos;
import com.imooc.pojo.vo.VideosVo;
import com.imooc.service.BgmService;
import com.imooc.service.VideoService;
import com.imooc.utils.FileUtils;
import com.imooc.utils.IMoocJSONResult;
import com.imooc.utils.PageResult;
import com.imooc.utils.ffmpeg.FFMpegDelAudioUtils;
import com.imooc.utils.ffmpeg.FFMpegVideoImageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Api(value = "视频相关", tags = {"视频相关"})
@RequestMapping("/video")
public class VideoController extends BaseController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private BgmService bgmService;
    @Value("${com.imooc.fileSpace}")
    private String fileSpace;
    @Value("${com.imooc.ffmpegSpace}")
    private String ffmpegSpace;

    @PostMapping(value = "/uploadVideo")
    @ApiOperation(value = "上传视频", notes = "上传视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "videoDesc", value = "videoDesc", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "audioId", value = "audioId", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "videoSeconds", value = "videoSeconds", required = true, dataType = "Float", paramType = "form"),
            @ApiImplicitParam(name = "videoWidth", value = "videoWidth", required = true, dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(name = "videoHeight", value = "videoHeight", required = true, dataType = "Integer", paramType = "form"),

    })
    public IMoocJSONResult uploadVideo(
            String userId,
            String audioId,
            String videoDesc,
            Float videoSeconds,
            Integer videoWidth,
            Integer videoHeight,
            @RequestParam("files") MultipartFile files) throws Exception {
        //判断传递的文件是不是空的
        if (files == null) {
            return IMoocJSONResult.errorMsg("请选择文件 ");
        }
        //数据库相对路径
        String uploadPathDB = "/" + userId + "/video";
        //返回原视频的保存地址
        String finaluploadPathDB = FileUtils.saveFileOne(files, fileSpace, uploadPathDB);
        //生成截图
        FFMpegVideoImageUtils ffMpegVideoImageUtils = new FFMpegVideoImageUtils();
        String coverPath = uploadPathDB + "/" + UUID.randomUUID().toString() + ".jpg";
        ffMpegVideoImageUtils.getImageFromVideo(ffmpegSpace,
                fileSpace + finaluploadPathDB,
                fileSpace + coverPath
        );
        //todo 判断背景音乐是否需要合并
        if (!StringUtils.isBlank(audioId)) {
            //视频文件的地址
            String videoPath = fileSpace + finaluploadPathDB;
            //查询出背景音乐
            Bgm bgm = bgmService.queryBgm(audioId);
            //背景音乐的地址
            String audioPath = fileSpace + bgm.getPath();
            //音视频合成
            finaluploadPathDB = uploadPathDB + "/" + UUID.randomUUID().toString() + ".mp4";
            FFMpegDelAudioUtils.convetor(ffmpegSpace,
                    videoPath, audioPath, videoSeconds, fileSpace + finaluploadPathDB
            );
        }
        //保存到数据库中
        Videos videos = new Videos();
        videos.setUserId(userId);
        videos.setAudioId(audioId);
        videos.setVideoPath(finaluploadPathDB);
        videos.setVideoDesc(videoDesc);
        videos.setVideoSeconds(videoSeconds);
        videos.setVideoWidth(videoWidth);
        videos.setVideoHeight(videoHeight);
        videos.setCoverPath(coverPath);
        // System.out.println("视频对象:" + videos.toString());
        videoService.saveVideos(videos);

        return IMoocJSONResult.ok(finaluploadPathDB);
    }


    @PostMapping("/uploadVideoCover")
    @ApiOperation(value = "上传视频封面", notes = "上传视频封面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String", paramType = "form"),
    })
    public IMoocJSONResult uploadVideoCover(
            String userId,
            @RequestParam("file") MultipartFile[] files) throws IOException {
        //判断传递的文件是不是空的
        if (files == null || files.length == 0) {
            return IMoocJSONResult.errorMsg("视频封面为空 ");
        }
        //数据库相对路径
        String uploadPathDB = "/" + userId + "/videoCover";
        uploadPathDB = FileUtils.saveFile(files, fileSpace, uploadPathDB);
        return IMoocJSONResult.ok(uploadPathDB);
    }


    @PostMapping("/list")
    @ApiOperation(value = "获取视频列表", notes = "获取视频列表的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isSaveRecord", value = "0:不保存  1保存", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "pageNum", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = true, dataType = "Integer", paramType = "query"),
    })
    public IMoocJSONResult list(
            @RequestBody Videos video,
            Integer isSaveRecord,
            Integer pageNum, Integer pageSize) {
        if (pageNum==null   )pageNum=1;
        if (pageSize==null   )pageNum=5;


        PageResult<VideosVo> result = videoService.list(
                video,
                isSaveRecord,
                pageNum, pageSize);

        return IMoocJSONResult.ok(result);
    }



    @PostMapping("/recordslist")
    @ApiOperation(value = "获取热搜列表", notes = "获取热搜列表")
    public IMoocJSONResult searchRecordsList( ) {
        return IMoocJSONResult.ok(videoService.searchRecordsList());
    }



}
